package com.yycx.module.bus.provider.handler.haiying.custombalance;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 退货款单保存
 *
 * @author zyf
 */
@Component
public class RefundHkPaymentSaveHandler implements SaveInterceptor<BusPayment> {


    @Resource
    private BusCustomBankService customBankService;

    @Resource
    private BusSalemanBankService salemanBankService;

    @Resource
    private BusinessUtils businessUtils;


    @Resource
    private BusPaymentService paymentService;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String selectIds = cs.getParams("selectIds");
        BusPayment old = paymentService.getById(selectIds);
        //客户收款银行
        Long bankId = cs.getLong("bankId");
        String refundAmount = cs.getParams("refundAmount");
        String amountType = cs.getParams("amountType");
        payment.setBankId(bankId);
        if (AmountTypeEnum.OrderAmount.getCode().equals(amountType)) {
            BusCustomBank customBank = customBankService.getById(bankId);
            if(FlymeUtils.isNotEmpty(customBank)) {
                payment.setBankNo(customBank.getBankNo());
                payment.setBankName(customBank.getBankName());
            }
            payment.setAmountType(AmountTypeEnum.RefundHk.getCode());
        }
        if (AmountTypeEnum.CustomBond.getCode().equals(amountType)) {
            BusSalemanBank salemanBank = salemanBankService.getById(bankId);
            if(FlymeUtils.isNotEmpty(salemanBank)) {
                payment.setBankNo(salemanBank.getBankNo());
                payment.setBankName(salemanBank.getBankName());
            }
            payment.setAmountType(AmountTypeEnum.CbReturn.getCode());
        }
        if (AmountTypeEnum.YfkAmount.getCode().equals(amountType)) {
            BusCustomBank customBank = customBankService.getById(bankId);
            if(FlymeUtils.isNotEmpty(customBank)) {
                payment.setBankNo(customBank.getBankNo());
                payment.setBankName(customBank.getBankName());
            }
            payment.setAmountType(AmountTypeEnum.RefundHk.getCode());
        }
        if (AmountTypeEnum.CustomRefund.getCode().equals(amountType)) {
            BusCustomBank customBank = customBankService.getById(bankId);
            payment.setBankNo(customBank.getBankNo());
            payment.setBankName(customBank.getBankName());
            payment.setAmountType(AmountTypeEnum.RefundHk.getCode());
        }
        String customId = old.getCustomId();
        //退款金额
        BigDecimal amount = new BigDecimal(refundAmount);
        Long userId = OpenHelper.getUserId();
        String paymentNo = businessUtils.getPaymentNo(old.getCompanyId(), userId);
        payment.setPaymentNo(paymentNo);
        payment.setSaleManId(old.getSaleManId());
        payment.setAmount(amount);
        payment.setParentId(old.getPaymentId());
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setCompanyId(OpenHelper.getCompanyId());
        payment.setCustomId(customId);
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setSelectIds(selectIds);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void prepare(CriteriaUpdate cu, EntityMap params, BusPayment payment) {

        SaveInterceptor.super.prepare(cu, params, payment);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {

    }

}
